#pragma once
#include "Precompiled.h"

namespace Sandbox
{
	class SkeletonStore
	{
		/*
		Purpose: This class extracts all the bones from the scene and 
		creates correspondent GPU resources and performs IK.
		*/

	public:

		struct int32x3
		{
			union { struct { int x,y,z; }; int i[3]; };

			int32 Max() { return max(max(x, y), z); }
			int32 Min() { return min(min(x, y), z); }

			int32x3 operator=( _In_ int32x3 o ) { x = o.x, y = o.y, z = o.z; return (*this); }
			int32x3 operator=( _In_ int32 i ) { x = i, y = i, z = i; return (*this); }
			int32 &operator[]( _In_ int32 j ) { return i[j]; }

			int32x3 operator*( _In_ int32 i ) 
			{ 
				int32x3 r = (*this); 
				r.x *= i; 
				r.y *= i; 
				r.z *= i; 
				return r;
			}

		};

		struct DummyPropertiesDims
		{
			int32x3 TranslationKeysCount;
			int32x3 RotationKeysCount;
			int32x3 ScaleKeysCount;
		};

		struct DummyInfo
		{
			//int32 Id;
			int32 ParentIndex;
			//int32 ParentsCount;
		};

		struct DummyWorld
		{
			Reign::Matrix CtrlWorld;
			Reign::Matrix NrmlWorld;
		};

	public:

		void Import( _In_ Eifersucht::ScenePtr scene );

	private:

		void Import( _In_ Eifersucht::SceneChildPtr root );

	public: // Device

		_Decl_value_prop_hinl1( m_d3dContext, Sehnsucht::Resource::Context3Ptr, Context3 );
		_Decl_value_prop_hinl1( m_d3dDevice, Sehnsucht::Resource::Device3Ptr, Device3 );

	private: // Device
		
		Sehnsucht::Resource::Context3Ptr m_d3dContext;
		Sehnsucht::Resource::Device3Ptr m_d3dDevice;

	private: // Resources

		// Read-only

		Sehnsucht::Utility::ReadResource<Sehnsucht::Resource::Buffer> m_bonesInfos; // dummy infos
		Sehnsucht::Utility::ReadResource<Sehnsucht::Resource::Buffer> m_bonesPropsDims; // dummy properties

		Sehnsucht::Utility::ReadResource<Sehnsucht::Resource::Texture2> m_dummiesVectors[3];
		Sehnsucht::Utility::ReadResource<Sehnsucht::Resource::Texture2> m_dummiesJoints[3];
		
		// Read-write

		Sehnsucht::Utility::ActiveResource<Sehnsucht::Resource::Buffer> m_bonesLclWorlds;
		Sehnsucht::Utility::ActiveResource<Sehnsucht::Resource::Buffer> m_bonesGlbWorlds;

		// Stages

		Sehnsucht::Resource::CShader m_evaluateLclWorlds;
		Sehnsucht::Resource::CShader m_evaluateGlbWorlds;

	}; TinyTypeDecl(SkeletonStore);
};